package com.weipai.springboot.module.dao.impl;

import com.weipai.springboot.enums.OrderStatus;
import com.weipai.springboot.model.*;
import com.weipai.springboot.module.dao.OrderDao;
import com.weipai.springboot.module.mapper.*;
import com.weipai.springboot.module.service.impl.OrderServiceImpl;
import com.weipai.springboot.util.PropUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-08-27 10:09
 **/
@Repository
public class OrderDaoImpl implements OrderDao {

    static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final Logger log = LoggerFactory.getLogger(OrderDaoImpl.class);

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderSublistMapper orderSublistMapper;
    @Autowired
    private SaleReturnMapper saleReturnMapper;
    @Autowired
    private ExchangeMapper exchangeMapper;
    @Autowired
    private InventoryOutMapper inventoryOutMapper;
    @Autowired
    private EmployeeIncomeMapper employeeIncomeMapper;

    @Override
    public List<Map<String, Object>> getOrderList(String shopId, String customerId, Integer status,
                                                  String orderId, String startTime, String endTime,
                                                  Integer start, Integer size, String keyword, Integer modeOfPayment) {
        return orderMapper.getOrderList(shopId, customerId, status, orderId, startTime, endTime, start, size, keyword, modeOfPayment);
    }

    @Override
    public Integer countOrderList(String shopId, String customerId, Integer status, String orderNumber, String startTime, String endTime) {
        return orderMapper.countOrderList(shopId, customerId, status, orderNumber, startTime, endTime);
    }

    @Override
    public Integer update(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<Map<String, Object>> getOrderSublist(String orderNumber) {
        return orderSublistMapper.getOrderSublist(orderNumber);
    }

    @Override
    public int deleteOrder(String orderNumber) {
        int r = 0;
        r = orderMapper.deleteOrder(orderNumber);
        orderSublistMapper.deleteByOrderNumber(orderNumber);
        return r;
    }

    @Override
    public List<Map<String, Object>> getOrderCountList(String time) {
        return orderMapper.getOrderCountList(time);
    }

    @Override
    public int insertOrder(Order order) {
        return orderMapper.insertSelective(order);
    }

    @Override
    public int insertSubOrderList(List<OrderSublist> sublist) {
        return orderSublistMapper.insertSubOrderList(sublist);
    }

    //根据订单号查询订单
    @Override
    public Map<String, Object> getOrderInfoByOrderNumber(String orderNumber) {
        return orderMapper.getOrderInfoByOrderNumber(orderNumber);
    }

    //保存退货申请
    @Override
    public int saveSaleReturn(SaleReturn saleReturn) {
        return saleReturnMapper.insertSelective(saleReturn);
    }

    //获取换货界面的商品信息、地址信息
    @Override
    public Map<String, Object> getOrderInfoByOrderNumberAndGoodsId(String orderNumber, String goodsId) {
        return orderMapper.getOrderInfoByOrderNumberAndGoodsId(orderNumber, goodsId);
    }

    //审核退货订单
    @Override
    public int auditReturnOrder(String auditUserId, String saleReturnNumber, Integer isAudit) {
        return saleReturnMapper.auditReturnOrder(auditUserId, saleReturnNumber, isAudit);
    }

    //更改退货订单的状态
    @Override
    public int updateSaleReturnStatus(String userId, String saleReturnNumber, int status) {
        return saleReturnMapper.updateSaleReturnStatus(userId, saleReturnNumber, status);
    }

    //保存换货申请
    @Override
    public int saveExchange(Exchange exchange) {
        return exchangeMapper.insert(exchange);
    }

    //换货申请的审核
    @Override
    public int auditExchange(String auditUserId, String exchangeNumber, Integer isAudit) {
        return exchangeMapper.auditExchange(auditUserId, exchangeNumber, isAudit);
    }

    //更改换货订单的状态
    @Override
    public int changeExchangeStatus(String userId, String exchangeNumber, Integer status) {
        return exchangeMapper.changeExchangeStatus(userId, exchangeNumber, status);
    }

    //获得用户全部的子订单
    @Override
    public List<Map<String, Object>> orderSublistByCustomerId(String customerId, Integer start, Integer size, Integer isAfterSale) {
        return orderMapper.orderSublistByCustomerId(customerId, start, size, isAfterSale);
    }

    //获取用户全部子订单的数量
    @Override
    public Integer orderSublistByCustomerIdAmount(String customerId, Integer isAfterSale) {
        return orderMapper.orderSublistByCustomerIdAmount(customerId, isAfterSale);
    }

    //点击售后申请跳转选择售后类型页面
    @Override
    public Map<String, Object> afterSaleTypeInfo(String orderSublistId) {
        return orderMapper.afterSaleTypeInfo(orderSublistId);
    }

    //确认发货
    @Override
    public int confirmDelivery(String orderNumber, String logisticsCompany, String trackingNumber) {
        return orderMapper.confirmDelivery(orderNumber, logisticsCompany, trackingNumber);
    }

    //获取子订单中的基本信息
    @Override
    public List<Map<String, Object>> getSublistByOrderNumber(String orderNumber) {
        return orderSublistMapper.getSublistByOrderNumber(orderNumber);
    }

    //批量保存出库明细
    @Override
    public int saveInventoryOutList(List<InventoryOut> outList) {
        return inventoryOutMapper.saveInventoryOutList(outList);
    }

    //更改订单状态
    @Override
    public int updatePaymentAndStatus(String orderNumber, Integer payment, int orderStatus, int paymentStatus) {
        return orderMapper.updatePaymentAndStatus(orderNumber, payment, orderStatus, paymentStatus);
    }

    //查询总共的交易金额
    @Override
    public Integer getAllRealPay(String startTime, String endTime) {
        return orderMapper.getAllRealPay(startTime, endTime);
    }

    //按月份和订单状态分组查出交易金额
    @Override
    public List<Map<String, Object>> orderStatement(String time) {
        return orderMapper.orderStatement(time);
    }

    //某一时间段的退款金额
    @Override
    public Integer getSaleReturnRealPay(String time) {
        return saleReturnMapper.getSaleReturnRealPay(time);
    }

    @Override
    public int getOrderByShopId(String shopId) {
        return orderMapper.getOrderByShopId(shopId);
    }

    @Override
    public Integer getSaleReturnRealPayByShopId(String shopId) {
        return saleReturnMapper.getSaleReturnRealPayByShopId(shopId);
    }

    //查询用户的下单金额和交易成功的金额
    @Override
    public Map<String, Object> getOrderAmountByCustomerId(String customerId, int status) {
        return orderMapper.getOrderAmountByCustomerId(customerId, status);
    }

    //查询用户的退货金额和退货金额
    @Override
    public Map<String, Object> getSaleReturnAmountByCustomerId(String customerId) {
        return saleReturnMapper.getSaleReturnAmountByCustomerId(customerId);
    }

    //查询退货订单
    @Override
    public List<Map<String, Object>> getSaleReturnList(Integer isAudit, Integer start, Integer size, String search,
                                                       String shopId, String startTime, String endTime) {
        return saleReturnMapper.getSaleReturnList(isAudit, start, size, search, shopId, startTime, endTime);
    }

    //查询退货订单数量
    @Override
    public int getSaleReturnAmount(Integer isAudit, String search, String shopId, String startTime, String endTime) {
        return saleReturnMapper.getSaleReturnAmount(isAudit, search, shopId, startTime, endTime);
    }

    //查询换货订单
    @Override
    public List<Map<String, Object>> getExchangeList(Integer isAudit, Integer start, Integer size, String search,
                                                     String shopId, String startTime, String endTime) {
        return exchangeMapper.getExchangeList(isAudit, start, size, search, shopId, startTime, endTime);
    }

    //查询换货订单的数量
    @Override
    public int getExchangeListAmount(Integer isAudit, String search, String shopId, String startTime, String endTime) {
        return exchangeMapper.getExchangeListAmount(isAudit, search, shopId, startTime, endTime);
    }

    //根据退货单号查询退货订单
    @Override
    public SaleReturn getSaleReturnBySaleReturnNumber(String saleReturnNumber) {
        return saleReturnMapper.getSaleReturnBySaleReturnNumber(saleReturnNumber);
    }

    //根据订单号获得子订单的信息
    @Override
    public List<Map<String, Object>> getSublist(String orderNumber) {
        return orderSublistMapper.getSublist(orderNumber);
    }

    @Override
    public int deleteByPrimaryKey(String orderId) {
        return orderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public int deleteSublistByOrderNumber(String orderId) {
        return orderSublistMapper.deleteByOrderNumber(orderId);
    }

    //查看此订单是否是信誉值支付的退货订单
    @Override
    public Map<String, Object> getSaleReturnBySaleReturnNumberAndPayment(String saleReturnNumber, Integer modeOfPayment) {
        return saleReturnMapper.getSaleReturnBySaleReturnNumberAadPayment(saleReturnNumber, modeOfPayment);
    }

    //更改订单的状态
    @Override
    public int changeOrderStatus(String orderNumber, int status) {
        return orderMapper.changeOrderStatus(orderNumber, status);
    }

    @Override
    public Order getOrderInfoByOrderNumber22(String orderNumber) {
        return orderMapper.getOrderInfoByOrderNumber22(orderNumber);
    }

    @Override
    public int updateByPrimaryKeySelective(Order order1) {
        return orderMapper.updateByPrimaryKeySelective(order1);
    }

    //更改订单状态为交易成功
    @Override
    public int updateOrderStatus(String orderNumber, int status) {
        return orderMapper.updateOrderStatus(orderNumber, status);
    }

    //根据状态查询订单
    @Override
    public List<Map<String, Object>> getOrderListByOrderStatus(int status) {
        return orderMapper.getOrderListByOrderStatus(status);
    }

    //通过订单号查询此订单 是否产生了员工收益
    @Override
    public EmployeeIncome getEmployeeIncomeByOrderNumber(String orderNumber) {
        return employeeIncomeMapper.getEmployeeIncomeByOrderNumber(orderNumber);
    }

    //查询用户订单每条订单退款金额
    public List<Map<String, Object>> getOrderByMoney(String customerId) {
        return orderMapper.getOrderByMoney(customerId);
    }

}
